package main

import (
	"fmt"
	"math"
)

//0ms 100%, 2.1MB 28%
func findContinuousSequence(target int) [][]int {
	if target < 3 {
		return nil
	}
	res := [][]int{}
	left, right := 1, 2.0
	for ; left <= target/2; left++ {
		right = -0.5 + math.Sqrt(float64(1+4*((left-1)*left+2*target)))/2
		if right == float64(int(right)) {
			res = append(res, []int{})
			for i := left; i <= int(right); i++ {
				res[len(res)-1] = append(res[len(res)-1], i)
			}
		}
	}
	return res
}

//双指针:4ms 48%, 2.1MB 28%
func findContinuousSequence1(target int) [][]int {
	if target < 3 {
		return nil
	}
	res := [][]int{}
	left, right := 1, 2
	for ; left <= target/2; left++ {
		for ; right <= target/2+1; right++ {
			if (left+right)*(right-left+1) == 2*target {
				res = append(res, []int{})
				for i := left; i <= int(right); i++ {
					res[len(res)-1] = append(res[len(res)-1], i)
				}
			}
			if (left+right)*(right-left+1) > 2*target {
				break
			}
		}
	}
	return res
}

func main() {
	fmt.Println(findContinuousSequence(5))
	fmt.Println(findContinuousSequence1(5))
}
